package cn.tedu.mimall.product.controller;

import cn.tedu.mimall.product.pojo.dto.AddressAddNewDTO;
import cn.tedu.mimall.product.pojo.dto.AddressUpdateDTO;
import cn.tedu.mimall.product.service.IAddressService;
import cn.tedu.mimall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/addresses")
@Slf4j
@Validated
@Api(tags = "收货地址控制器")
public class AddressController {

    @Autowired
    IAddressService addressService;

    public AddressController() {
        log.debug("收货地址控制器AddressController，启动！");
    }

    @ApiOperation("添加收货地址")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add")
    public JsonResult<Void> addAddress(AddressAddNewDTO addressAddNewDTO){
        log.debug("开始处理【添加收货地址】的请求，参数：{}",addressAddNewDTO);
        addressService.addAddress(addressAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除收货地址")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(value = "收货地址id",name = "id",required = true,dataType = "Long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理【删除收货地址】的请求，参数：{}",id);
        addressService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改收货地址")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateInfoById(@PathVariable Long id, AddressUpdateDTO addressUpdateDTO){
        log.debug("开始处理【修改收货地址】的请求，参数：{}，{}",id,addressUpdateDTO);
        addressService.updateInfoById(id,addressUpdateDTO);
        return JsonResult.ok();
    }

}
